package com.mallcai.bigdata.ladon.dpl.mapper.peekdata.provider;

import com.mallcai.bigdata.ladon.dpl.entity.peekdata.PeekField;
import com.mallcai.bigdata.ladon.dpl.mapper.BaseSqlProvider;
import com.mallcai.bigdata.ladon.dpl.mapper.system.provider.UserSqlProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author feixy
 * @version 1.0
 * @date 2019-07-08 17:06
 */
public class PeekFieldSqlProvider extends BaseSqlProvider<PeekField> {

    public static final String TB_PEEK_FIELD = "pd_peek_field";

    @Override
    public String getTableName() {
        return TB_PEEK_FIELD;
    }

    @Override
    public Map<String, String> getInsertColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("peek_id", "#{peekId}");
        map.put("meta_id", "#{metaId}");
        map.put("agg_expression", "#{aggExpression}");
        map.put("format", "#{format}");
        map.put("archive", "0");
        map.put("type",  "#{type}");
        map.put("alias",  "#{alias}");
        map.put("agg_data_type",  "#{aggDataType}");
        return map;
    }

    @Override
    public Map<String, String> getUpdateColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("metaId", "meta_id");
        map.put("aggExpression", "agg_expression");
        map.put("format", "format");
        map.put("type",  "type");
        map.put("alias",  "alias");
        map.put("aggDataType",  "agg_data_type");
        return map;
    }

    public String getByPeekId(@Param("peekId") Long peekId) {
        return new SQL() {
            {
                SELECT("tb_p.*, tb_mm.data_type, tb_mm.original_data_type,tb_mm.name as meta_name,tb_mm.show_name as meta_show_name, tb_u.nickname AS creator");
                FROM(TB_PEEK_FIELD + " tb_p");
                INNER_JOIN(ModelMetaSqlProvider.TB_MODEL_META + " tb_mm ON tb_mm.id = tb_p.meta_id ");
                INNER_JOIN(UserSqlProvider.TB_USER + " tb_u ON tb_u.id = tb_p.created_by ");
                WHERE(" tb_p.peek_id = #{peekId} and tb_p.archive = 0");

            }
        }.toString();
    }
}
